//
//  UIBarButtonItem+XYAdd.m
//  PeiJue
//
//  Created by lxy on 2017/9/27.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "UIBarButtonItem+XYAdd.h"

@implementation UIBarButtonItem (XYAdd)
+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName target:(id)target action:(SEL)action {
    return [self itemWithImageName:imageName highlightImageName:nil selectedImageName:nil target:target action:action];
}

+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highlightImageName:(nullable NSString *)highlightImageName target:(id)target action:(SEL)action {
    return [self itemWithImageName:imageName highlightImageName:highlightImageName selectedImageName:nil target:target action:action];
}

+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highlightImageName:(nullable NSString *)highlightImageName selectedImageName:(nullable NSString *)selectedImageName target:(id)target action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if (imageName) {
        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (highlightImageName) {
        [button setBackgroundImage:[UIImage imageNamed:highlightImageName] forState:UIControlStateHighlighted];
    }else {
        button.adjustsImageWhenHighlighted = NO;
    }
    
    if (selectedImageName) {
        [button setBackgroundImage:[UIImage imageNamed:highlightImageName] forState:UIControlStateSelected];
    }
    
    button.frame = (CGRect){CGPointZero,button.currentBackgroundImage.size};
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc]initWithCustomView:button];
}


@end
